package com.yltrcc.blog.business.impl;

import com.yltrcc.blog.model.model.JsonRootBean;
import com.yltrcc.blog.business.WeChatEntEntService;
import com.yltrcc.blog.generate.domain.Question;
import com.yltrcc.blog.business.ArticleService;
import com.yltrcc.blog.business.QuestionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.*;


/**
 * <a href="https://developer.work.weixin.qq.com/document/path/91770">企业微信机器人配置说明文档</a>
 */
@Service
public class WeChatEntEntServiceImpl extends WeChatEntBaseServiceImpl implements WeChatEntEntService {

    /**
     * 日志
     **/
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private QuestionService questionService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private RestTemplate restTemplate;

    @Override
//    @Scheduled(cron = "0 0 10,15,22,23 * * ?", zone = "Asia/Shanghai")
    public void sendRandomArticle() {
        ArrayList<String> mobileList = new ArrayList<>();
        mobileList.add("@All");
        log.debug("sendRandomArticle 执行开始。执行时间：" + new Date());
        String content = getRandomContent();

        sendMdMsg("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=8c416c63-a4b6-474d-8ff1-a040357f5595&debug=1",
                content, mobileList);
    }

    private String getRandomContent() {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();

        //每日一句
        JsonRootBean user = restTemplate.getForObject("https://v1.hitokoto.cn/", JsonRootBean.class);
        sb.append("# 每日一句\n\n");
        sb.append(user.getHitokoto());
        sb.append("\n\n");
        //面试题
        int count = questionService.getCounts();

        int nextInt = random.nextInt(count);
        nextInt = nextInt == 0 ? 1 : nextInt;
        System.out.println("nextInt = " + nextInt);
        Question question = questionService.findByQuestionId(nextInt);
        sb.append("\n\n\n# 面试题\n");
        sb.append(question.getArticleContentMd());
        sb.append("\n\n");
       /* for (int i = 0; i < 1; i++) {
            //获取随机数
            int i2 = random.nextInt(count);
            i2 = i2 == 0 ? 1 : i2;
            System.out.println("i2 = " + i2);
            Question question1 = questionService.findByQuestionId(i2);
            sb.append(question1.getArticleContentMd());
            sb.append("\n\n");
        }*/
        //文末微信公众号推广连接
        sb.append("\n\n> 你好，我是yltrcc，日常分享技术点滴，欢迎关注我：[ylcoder](https://mp.weixin.qq.com/s/-GWOmQA5QFMF0OLcnGk7qQ)");
        sb.append("\n\n> 你好，我是yltrcc，日常分享技术点滴，欢迎关注我：[每日任务完成情况汇总](http://43.136.130.33:10001/daily-summary)");
        sb.append("\n\n> 你好，我是yltrcc，日常分享技术点滴，欢迎关注我：[任务详情](http://43.136.130.33:10001/task-details)");
        return sb.toString();

    }
}
